<?php

if(!POST) {
} elseif(!FILE) {
    switch ($params[2]){
        // MEDIA ALT
        case 'alt':
            if($_POST['ID'] && $page->page->media->$_POST['ID']) {
                $page->page->media->$_POST['ID']->alt = $_POST['value'];
                uygulama::set('data/'.$params[1].'/content', $page->page);
                $page = uygulama::result('200',$_POST);
            } else $page = uygulama::result('600',uygulama::translate('Media item not found!'));
        break;
        // MEDIA REMOVE
        case 'removeMedia':
            @unlink('data/'.$params[1].'/m'.$_POST['ID'].'.jpg');
            @unlink('data/'.$params[1].'/t'.$_POST['ID'].'.jpg');
            if($_POST['ID'] && $page->page->media->$_POST['ID']) {
                unset($page->page->media->$_POST['ID']);
                uygulama::set('data/'.$params[1].'/content', $page->page);
                $page = uygulama::result('200');
            } else $page = uygulama::result('600',uygulama::translate('Media item not found!'));
        break;
        // CONTENT SAVE
        default:

         

            $lang = LANG;
            $page->page->$lang->title = $_POST['title'] ? $_POST['title'] : $page->page->$lang->title;
            $page->page->$lang->description = $_POST['description'] ? $_POST['description'] : $page->page->$lang->description;
            $page->page->$lang->keywords = $_POST['keywords'] ? $_POST['keywords'] : $page->page->$lang->keywords;
            $page->page->$lang->content = $_POST['content'] ? $_POST['content'] : $page->page->$lang->content;
            $page->page->recipient = $_POST['recipient'] ? $_POST['recipient'] : $page->page->recipient;
            $links =  array();
            foreach ($_POST['link-title'] as $k=>$i){
                if($_POST['link-url'][$k] && $_POST['link-url'][$k] != 'http://')
                    $links[] = array(
                                'title'=>($i?$i:$_POST['link-url'][$k]),
                                'url'=>$_POST['link-url'][$k]
                        );
            }
            $page->page->$lang->links = $links;   
            uygulama::set('data/'.$params[1].'/content', $page->page);
            $page = uygulama::result('200',sprintf(_('"%s" subpage is saved!'),$params[1]));
        break;
    }
} else {	
    $ext = '.'.end(explode(".",strtolower($_FILES['Filedata']['name'])));
    // MEDIA ID
    $ID = (@count($page->page->media) +1).(microtime(1)*10000);
    //if(!$page->page->media) $page->page->media = new stdClass();
    $page->page->media->$ID->type ='image';
    $page->page->media->$ID->thumb = 'data/'.$params[1].'/t'.$ID.'.jpg';
    $page->page->media->$ID->alt = $_FILES['Filedata']['name'];
    $page->page->media->$ID->src = 'data/'.$params[1].'/m'.$ID.'.jpg';
    $page->page->media->$ID->tmp = 'data/'.$params[1].'/m'.$ID.$ext;
    // MEDIA UPLOAD
    uygulama::upload($_FILES['Filedata']['tmp_name'], $page->page->media->$ID->tmp);
    // if type == image
    uygulama::imageresize($page->page->media->$ID->tmp, $page->page->media->$ID->src, 905);
    uygulama::imageresize($page->page->media->$ID->src, $page->page->media->$ID->thumb, 160, 90, true);
    // Delete tmp file
    if($page->page->media->$ID->tmp != $page->page->media->$ID->src)
    	@unlink($page->page->media->$ID->tmp);
    // Clear tmp
    unset($page->page->media->$ID->tmp);
    // SAVE CHANGES
    uygulama::set('data/'.$params[1].'/content', $page->page);
    // Set ID
    $page->page->media->$ID->ID = $ID;
    // Uploaded media data
    echo json_encode($page->page->media->$ID); exit;
}
?>
